﻿using System;
using System.Diagnostics;
using System.Configuration;

namespace TQB.Page.Config
{
    public class ConfigItem : ConfigurationElement
    {
        private static readonly ConfigurationProperty _method = new ConfigurationProperty("methodName", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty _name = new ConfigurationProperty("name", typeof(string), string.Empty, ConfigurationPropertyOptions.IsKey);

        private static readonly ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

        [ConfigurationProperty("methodName", IsRequired = true)]
        public string MethodName
        {
            get
            {
                return (string)base[_method];
            }
        }

        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get
            {
                return (string)base[_name];
            }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        static ConfigItem()
        {
            _properties.Add(_method);
            _properties.Add(_name);
        }

    }

    public class ConfigItemCollection : ConfigurationElementCollection
    {
        private static readonly ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

        public ConfigItem this[int index]
        {
            get { return (ConfigItem)base.BaseGet(index); }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }

                base.BaseAdd(index, value);
            }
        }

        public new ConfigItem this[string name]
        {
            get
            {
                return (ConfigItem)base.BaseGet(name);
            }
        }

        protected override string ElementName
        {
            get
            {
                //return base.ElementName;
                return "item";
            }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        public void Add(ConfigItem item)
        {
            base.BaseAdd(item);
        }

        public void Remove(ConfigItem item)
        {
            base.BaseRemove(item);
        }

        public void RemoveAt(int index)
        {
            base.BaseRemoveAt(index);
        }

        protected override ConfigurationElement CreateNewElement()
        {
            //throw new NotImplementedException();
            return new ConfigItem();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            //throw new NotImplementedException();
            return (element as ConfigItem).MethodName;
        }
    }

    public class HandlerTQBSection : ConfigurationSection
    {
        public const string SectionName = "handlerTQB";

        private static readonly ConfigurationProperty _items = new ConfigurationProperty(string.Empty, typeof(ConfigItemCollection), null, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection);
        private static readonly ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

        public ConfigItemCollection Items
        {
            get
            {
                return (ConfigItemCollection)base[_items];
            }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        static HandlerTQBSection()
        {
            _properties.Add(_items);
        }

    }

    public class _TestConfig
    {
        public _TestConfig()
        {
            HandlerTQBSection o = new HandlerTQBSection();
            o = (HandlerTQBSection)ConfigurationManager.GetSection("handlerTQB");

            ConfigItemCollection xc = new ConfigItemCollection();
            ConfigItem item = xc["ddd"];

        }
    }
}
